package com.liy.framework.common.openoffice;

import java.io.*;
import org.slf4j.*;

public class OpenOfficeUtil
{
    protected static Logger log;
    private String ip;
    private int port;
    
    public OpenOfficeUtil(final String openofficeHome) {
        this.ip = "127.0.0.1";
        this.port = 8100;
        OpenOfficeUtil.log.info("office: " + openofficeHome);
        if (openofficeHome.indexOf(":") > 0) {
            this.ip = openofficeHome.split(":")[0];
            this.port = Integer.parseInt(openofficeHome.split(":")[1]);
        }
    }
    
    public boolean convert2Pdf(final String inputFilePath, final String outputFilePath) throws Exception {
        final long s = System.currentTimeMillis();
        OpenOfficeUtil.log.info("start convert office inputFilePath: " + inputFilePath);
        final File file = new File(outputFilePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
            OpenOfficeUtil.log.info("File dir not exist, create dir: " + file.getParentFile().getAbsolutePath());
        }
        OpenOfficeTool.convert(inputFilePath, outputFilePath, this.ip, this.port);
        OpenOfficeUtil.log.info("convert office time: " + (System.currentTimeMillis() - s));
        return true;
    }
    
    static {
        OpenOfficeUtil.log = LoggerFactory.getLogger((Class)OpenOfficeUtil.class);
    }
}
